package net.oschina.jhssm.module.system.controller;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import net.oschina.jhssm.common.entity.R;
import net.oschina.jhssm.module.system.dto.JsTreeNode;
import net.oschina.jhssm.module.system.dto.JsTreeNode.State;
import net.oschina.jhssm.module.system.entity.SysResource;
import net.oschina.jhssm.module.system.query.SysResourceQuery;
import net.oschina.jhssm.module.system.service.ISysResourceService;

/**
 * Created by zhaojb on 2017/8/20.
 */
@Controller
@RequestMapping(value = "setting/resources")
public class SysResourceController {

    @Autowired
    private ISysResourceService resourceService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(Model model) {
    	model.addAttribute("pageHeader", "资源管理");
        return "resource/index";
    }
    
    @RequestMapping(value = "createResourceTree", method = RequestMethod.GET)
    @ResponseBody
    public R createResourceTree() {
    	List<SysResource> resources = resourceService.selectAll();
    	List<JsTreeNode> jsTree = new ArrayList<>();
    	for (SysResource sysResource : resources) {
    		if ("BUTTON".equals(sysResource.getType())) {
    			continue;
    		}
    		JsTreeNode node = new JsTreeNode();
    		node.setId(sysResource.getId());
    		node.setParent(sysResource.getParentId());
    		node.setText(sysResource.getName());
    		node.setIcon(sysResource.getIcon());
    		node.setType(sysResource.getType());
    		if ("DIR".equals(sysResource.getType())) {
    			State state = new State();
    			state.setOpened(true);
    			node.setState(state);
    		}
    		jsTree.add(node);
    	}
		return R.ok("tree", jsTree);
	}
    
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public R list(SysResourceQuery query) {
    	SysResource resource = new SysResource();
    	BeanUtils.copyProperties(query, resource, "offset", "limit");
    	PageHelper.startPage(query.getPage(), query.getLimit());
    	List<SysResource> resources = resourceService.select(resource);
    	PageInfo<SysResource> resourcePage = new PageInfo<>(resources);
    	return R.ok("data", resourcePage.getList(), "count", resourcePage.getTotal());
    }
    
    @RequestMapping(value="new", method=RequestMethod.GET)
    public String new0() {
    	return "form";
    }
    
    @RequestMapping(value="", method=RequestMethod.POST)
    @ResponseBody
    public R create(SysResource resource) {
    	resource.setGmtCreate(new DateTime().toDate());
    	int row = resourceService.insertSelective(resource);
    	return R.ok("row", row);
    }
    
    public R update(SysResource resource) {
    	return R.ok();
    }

}
